package com.aiyige.page.publish.inputintroduction;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.StringUtils;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.InputIntroductionPage)
public class InputIntrodutionPage extends AppCompatActivity {
    public static final int DEFAULT_MAX_INPUT_NUM = 5000;
    public static final String EXTRA_KEY_INPUT_STRING = "com.aiyige.extra.EXTRA_KEY_INPUT_STRING";
    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.inputEt)
    EditText inputEt;
    @BindView(R.id.inputNumTv)
    TextView inputNumTv;
    @BindView(R.id.maxInputNumTv)
    TextView maxInputNumTv;
    InputNumFilter inputNumFilter;
    @Autowired
    String text;
    @Autowired
    int maxInputNum = 0;
    @Autowired
    String title;
    @Autowired
    String hint;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_input_introdution);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        if (maxInputNum == 0) {
            maxInputNum = DEFAULT_MAX_INPUT_NUM;
        }
        if (TextUtils.isEmpty(title)) {
            title = StringUtils.getString(R.string.edit_brief_introduction);
        }
        if (TextUtils.isEmpty(hint)) {
            hint = StringUtils.getString(R.string.please_input_introduction);
        }
        titleTv.setText(title);
        titleActionBtn.setText(R.string.finish);
        inputNumFilter = new InputNumFilter(maxInputNum);
        inputNumTv.setText("0");
        maxInputNumTv.setText(String.valueOf(maxInputNum));
        inputNumFilter.setListener(new InputNumFilter.Listener() {
            @Override
            public void inputNum(int num) {
                inputNumTv.setText(String.valueOf(num));
            }
        });
        inputEt.setFilters(new InputFilter[]{inputNumFilter});
        inputEt.setText(text);
        inputEt.setSelection(inputEt.getText().length());
        inputEt.setHint(hint);
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn: {
                setResult(RESULT_CANCELED);
                finish();
                break;
            }
            case R.id.titleActionBtn: {
                Intent intent = new Intent();
                intent.putExtra(EXTRA_KEY_INPUT_STRING, inputEt.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
                break;
            }
        }
    }
}
